package zjc.bookkeeping.homepage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import zjc.bookkeeping.R;
import zjc.bookkeeping.adapter.ConsumptionListAdapter;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.homepage.addpage.AddActivity;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.tools.SelectClassDialog;
import zjc.bookkeeping.tools.SelectTimeDialog;
import zjc.bookkeeping.tools.ToolsClass;

public class ShowConsumptionList extends AppCompatActivity implements View.OnClickListener {
    ImageView backIv;
    TextView accountTv,outMoneyTv,inMoneyTv,transferMoneyTv,timeTv,classTv;
    LinearLayout classLL,timeLL;
    ListView listView;
    String selectTime = ToolsClass.getYMTime(0);
    String selectClass = "全部";
    List<ConsumptionBean> consumptionBeanList;
    DecimalFormat df = new DecimalFormat("#0.00");// 保留两位小数
    ConsumptionListAdapter consumptionListAdapter;
    int accountId = DBManager.selectAccountId();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_consumption_list);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataAdapter();
    }

    private void initView() {
        backIv = findViewById(R.id.show_list_back);
        accountTv = findViewById(R.id.show_tv_account);
        accountTv.setText(DBManager.getAccountName(accountId));
        outMoneyTv = findViewById(R.id.show_tv_outcome_money);
        inMoneyTv = findViewById(R.id.show_tv_income_money);
        transferMoneyTv = findViewById(R.id.show_tv_transfer_money);
        timeTv = findViewById(R.id.show_tv_time);
        timeTv.setText(selectTime);
        classTv = findViewById(R.id.show_tv_class);
        timeLL = findViewById(R.id.show_ll_time);
        classLL = findViewById(R.id.show_ll_class);
        listView = findViewById(R.id.show_lv_list);

        backIv.setOnClickListener(this);
        timeLL.setOnClickListener(this);
        classLL.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.putExtra("consumptionBean", consumptionBeanList.get(i));
                intent.setClass(ShowConsumptionList.this,ShowConsumption.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.show_list_back:
                finish();
                break;
            case R.id.show_ll_class:
                showSelectClass();
                break;
            case R.id.show_ll_time:
                showSelectTime();
                break;
        }
    }

    //弹出时间选择Dialog
    private void showSelectTime() {
        SelectTimeDialog selectTimeDialog = new SelectTimeDialog(this);
        selectTimeDialog.show();
        selectTimeDialog.setRemarksDialogSize();
        selectTimeDialog.setOnTimeSelectSureListener(new SelectTimeDialog.OnTimeSelectSureListener() {
            @Override
            public void onTimeSelectSure(String time, int year, int month, int day) {
                timeTv.setText(time);
                selectTime = time;
                getDataAdapter();
            }
        });
    }
//获取数据和listview样式
    private void getDataAdapter() {
        consumptionBeanList = new ArrayList<>();
        List<ConsumptionBean> list = DBManager.getAllAccountConsumption(accountId+"",selectClass,selectTime);
        consumptionBeanList.addAll(list);
        consumptionListAdapter = new ConsumptionListAdapter(this,consumptionBeanList);
        listView.setAdapter(consumptionListAdapter);
        updateMoney();
    }
//    更新显示的金额
    private void updateMoney() {
        float outcomeMoney = 0;
        float incomeMoney = 0;
        float transferMoney = 0;
        for (int i = 0;i<consumptionBeanList.size();i++){
            switch (consumptionBeanList.get(i).getConsumption_kind()){
                case 0:
                    outcomeMoney += consumptionBeanList.get(i).getConsumption_money();
                    break;
                case 1:
                    incomeMoney += consumptionBeanList.get(i).getConsumption_money();
                    break;
                case 2:
                    transferMoney += consumptionBeanList.get(i).getConsumption_money();
                    break;
            }
            Log.i("getConsumption_money("+i+")",consumptionBeanList.get(i).getConsumption_money()+"");
        }
        outMoneyTv.setText(df.format(outcomeMoney));
        inMoneyTv.setText(df.format(incomeMoney));
        transferMoneyTv.setText(df.format(transferMoney));
    }

    //    弹出消费类型选择Dialog
    private void showSelectClass() {
        SelectClassDialog selectClassDialog = new SelectClassDialog(this);
        selectClassDialog.show();
        selectClassDialog.setClassDialogSize();
        selectClassDialog.setOnSelectClassListener(new SelectClassDialog.OnSelectClassListener() {
            @Override
            public void onSelectClassSure(String name) {
                classTv.setText(name);
                selectClass = name;
                getDataAdapter();
            }
        });
    }
}